<?php


namespace addon\hotel\hotel\controller;

use addon\hotel\model\HotelSettlement;
use app\model\hotel\Hotel as HotelModel;
use addon\hotel\model\HotelAccount as HotelAccountModel;
use addon\hotel\model\HotelSettlement as HotelSettlementModel;
use addon\hotel\model\OrderCommon as OrderCommonModel;

class Account extends BaseHotel
{
    public function __construct()
    {
        //执行父类构造函数
        parent::__construct();
    }

    /**
     * 店铺账户面板
     */
    public function dashboard()
    {
        $shop_model = new HotelModel();
        $shop_account_model = new HotelAccountModel();

        //获取商家转账设置
        $shop_withdraw_config = $shop_account_model->getHotelWithdrawConfig();
        $this->assign('shop_withdraw_config', $shop_withdraw_config[ 'data' ][ 'value' ]);//商家转账设置

        //获取店铺的账户信息
        $condition = array (
            [ "hotel_id", "=", $this->hotel_id ]
        );
        $shop_info_result = $shop_model->getHotelInfo($condition, 'hotel_name,hotel_img,account, account_withdraw,account_withdraw_apply');
        $shop_info = $shop_info_result[ "data" ];
        $this->assign('shop', $shop_info);
        //余额
        $account = $shop_info[ 'account' ] - $shop_info[ 'account_withdraw_apply' ];
        $this->assign('account', number_format($account, 2, '.', ''));
        //累计收入
        $total = $shop_info[ 'account' ] + $shop_info[ 'account_withdraw' ];
        $this->assign('total', number_format($total, 2, '.', ''));
        //已提现
        $this->assign('account_withdraw', number_format($shop_info[ 'account_withdraw' ], 2, '.', ''));
        //提现中
        $this->assign('account_withdraw_apply', number_format($shop_info[ 'account_withdraw_apply' ], 2, '.', ''));


        //店铺的待结算金额
        $settlement_model = new HotelSettlementModel();
        $settlement_info = $settlement_model->getWaitSettlementInfo($this->hotel_id);
        $order_apply = $settlement_info[ 'hotel_money' ] - $settlement_info[ 'refund_hotel_money' ] - $settlement_info[ 'commission' ];
        $this->assign('order_apply', number_format($order_apply, 2, '.', ''));

        //账户收支
        if (request()->isAjax()) {

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);

            $condition[] = [ 'hotel_id', '=', $this->hotel_id ];
            $type = input('type', '');//收支类型（1收入  2支出）
            if (!empty($type)) {
                switch ( $type ) {
                    case 1:
                        $condition[] = [ 'account_data', '>', 0 ];
                        break;
                    case 2:
                        $condition[] = [ 'account_data', '<', 0 ];
                        break;
                }
            }
            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = [ 'create_time', '>=', $start_time ];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = [ 'create_time', '<=', $end_time ];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = [ 'create_time', 'between', [ $start_time, $end_time ] ];
            }

            $field = 'account_no,hotel_id,account_type,account_data,from_type,type_name,relate_tag,create_time,remark';
            return $shop_account_model->getAccountPageList($condition, $page, $page_size, 'id desc', $field);
        }

        return $this->fetch('account/dashboard', [], $this->replace);
    }

    /**
     * 账户交易记录
     */
    public function orderList()
    {
        //店铺的待结算金额
        $settlement_model = new HotelSettlement();
        $settlement_info = $settlement_model->getWaitSettlementInfo($this->hotel_id);
        $wait_settlement = $settlement_info[ 'hotel_money' ] - $settlement_info[ 'refund_hotel_money' ] - $settlement_info[ 'commission' ];
        $this->assign('wait_settlement', number_format($wait_settlement, 2, '.', ''));
        //店铺的已结算金额
        $finish_condition = [
            [ 'hotel_id', '=', $this->hotel_id ],
            [ 'order_status', '=', 10 ],
            [ 'settlement_id', '>', 0 ]
        ];
        $settlement_info = $settlement_model->getShopSettlementData($finish_condition);
        $finish_settlement = $settlement_info[ 'hotel_money' ] - $settlement_info[ 'refund_hotel_money' ] - $settlement_info[ 'commission' ];
        $this->assign('finish_settlement', number_format($finish_settlement, 2, '.', ''));
        //店铺的进行中金额
        $settlement_condition = [
            [ 'hotel_id', '=', $this->hotel_id ],
            [ 'order_status', "not in", [ 0, -1, 10 ] ]
        ];
        $settlement_info = $settlement_model->getShopSettlementData($settlement_condition);
        $settlement = $settlement_info[ 'hotel_money' ] - $settlement_info[ 'refund_hotel_money' ] - $settlement_info[ 'commission' ];
        $this->assign('settlement', number_format($settlement, 2, '.', ''));

        if (request()->isAjax()) {

            $order_model = new OrderCommonModel();
            $condition[] = [ 'hotel_id', '=', $this->hotel_id ];

            //下单时间
            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = [ "finish_time", ">=", $start_time ];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = [ "finish_time", "<=", $end_time ];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = [ 'finish_time', 'between', [ $start_time, $end_time ] ];
            }

            //订单状态
            $order_status = input("order_status", "");
            if ($order_status != "") {
                switch ( $order_status ) {
                    case 1://进行中

                        $condition[] = [ "order_status", "not in", [ 0, -1, 10 ] ];
                        $order = 'pay_time desc';
                        break;
                    case 2://待结算

                        $condition[] = [ "order_status", "=", 10 ];
                        $condition[] = [ "is_settlement", "=", 0 ];
                        $order = 'finish_time desc';
                        break;
                    case 3://已结算

                        $condition[] = [ "order_status", "=", 10 ];
                        $condition[] = [ "settlement_id", ">", 0 ];
                        $order = 'finish_time desc';
                        break;
                    case 4://全部
                        $condition[] = [ "order_status", "not in", [ 0, -1 ] ];
                        $order = 'pay_time desc';
                        break;
                }
            } else {
                $condition[] = [ "order_status", "=", 10 ];
                $condition[] = [ "settlement_id", "=", 0 ];
                $order = 'finish_time desc';
            }
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $field = 'order_id,order_no,order_money,order_status_name,hotel_money,platform_money,refund_money,refund_hotel_money,refund_platform_money,commission,finish_time,settlement_id';
            $list = $order_model->getOrderPageList($condition, $page, $page_size, $order, $field);

            return $list;
        }
        return $this->fetch('account/order_list', [], $this->replace);
    }
}